package com.edu.admin.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.edu.admin.entity.SysClasses;
import com.edu.admin.enums.ExceptionEnum;
import com.edu.admin.service.ClassesService;
import com.edu.admin.service.SchoolService;
import com.edu.admin.common.RestResult;
import com.edu.admin.vo.ClassesSubjectVo;
import com.edu.admin.vo.ClassesTeacherVo;
import com.edu.admin.vo.ClassesVo;
import com.github.pagehelper.PageInfo;

/**
 * 班级管理
 *  
 * @author Wangjiong
 * 
 */
@RestController
@RequestMapping(value = "/classes", name = "班级管理")
public class ClassesController {

	@Autowired
	private ClassesService	classesService;
	@Autowired
	private SchoolService	schoolService;
	
	/**
	 *
	 * @apiGroup classes
	 * @api {get} /classes/list  获取班级列表
	 * @apiVersion 1.0.0
	 * @apiName getClassesList
	 *
	 * @apiParam {Long} schoolId 学校id
	 * @apiParam {Long} phaseId 学段id
	 * @apiParam {Long} gradeId 年级id
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": [
		        {
		            "classesId": 1,
		            "classesNo": 1,
		            "classesName": "八年级1班",
		            "personalityName": "实验班",
		            "schoolId": 1,
		            "phaseId": 2,
		            "gradeId": 8,
		            "enterYear": "2018",
		            "graduateYear": "2021",
		            "isGraduated": 0,
		            "teacherId": 1,
		            "useType": 0,
		            "phaseName": "初中",
		            "gradeName": "八年级"
		        },
		        {
		            "classesId": 2,
		            "classesNo": 2,
		            "classesName": "八年级2班",
		            "personalityName": "火箭班",
		            "schoolId": 1,
		            "phaseId": 2,
		            "gradeId": 8,
		            "enterYear": "2018",
		            "graduateYear": "2021",
		            "isGraduated": 0,
		            "teacherId": null,
		            "useType": 0,
		            "phaseName": "初中",
		            "gradeName": "八年级"
		        }
		    ]
		}
	 *
	 *
	 */
    @RequestMapping(value = "list", name = "班级列表", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/classes/list')")
    public RestResult getClassesList(@RequestParam(value = "schoolId", required = false) Long schoolId,
									@RequestParam(value = "phaseId", required = false) Long phaseId,
									@RequestParam(value = "gradeId", required = false) Long gradeId)
						    		throws Exception {
    	
    	List<ClassesVo> clessesList = classesService.getClassesList(schoolId, phaseId, gradeId);
        
        return RestResult.ok(clessesList);
    }
    
    /**
	 *
	 * @apiGroup classes
	 * @api {get} /classes/listAllPage  获取所有班级列表（包括停用班级）（分页）
	 * @apiVersion 1.0.0
	 * @apiName getAllClassesListPage
	 *
	 * @apiParam {Long} schoolId 学校id  required = false
	 * @apiParam {Long} phaseId 学段id  required = false
	 * @apiParam {Long} gradeId 年级id  required = false
	 * @apiParam {Integer} isGraduated 是否毕业  required = false
	 * @apiParam {Integer} pageNum 当前页码  required = false
	 * @apiParam {Integer} pageSize 每页行数  required = false
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "total": 2,
		        "list": [
		            {
		                "classesId": 1,
		                "classesNo": 1,
		                "classesName": "八年级1班",
		                "personalityName": "实验班",
		                "schoolId": 1,
		                "phaseId": 2,
		                "gradeId": 8,
		                "enterYear": "2018",
		                "graduateYear": "2021",
		                "isGraduated": 0,
		                "teacherId": 1,
		                "useType": 0,
		                "phaseName": "初中",
		                "gradeName": "八年级"
		            },
		            {
		                "classesId": 2,
		                "classesNo": 2,
		                "classesName": "八年级2班",
		                "personalityName": "火箭班",
		                "schoolId": 1,
		                "phaseId": 2,
		                "gradeId": 8,
		                "enterYear": "2018",
		                "graduateYear": "2021",
		                "isGraduated": 0,
		                "teacherId": null,
		                "useType": 0,
		                "phaseName": "初中",
		                "gradeName": "八年级"
		            }
		        ],
		        "pageNum": 1,
		        "pageSize": 10,
		        "size": 2,
		        "startRow": 1,
		        "endRow": 2,
		        "pages": 1,
		        "prePage": 0,
		        "nextPage": 0,
		        "isFirstPage": true,
		        "isLastPage": true,
		        "hasPreviousPage": false,
		        "hasNextPage": false,
		        "navigatePages": 8,
		        "navigatepageNums": [
		            1
		        ],
		        "navigateFirstPage": 1,
		        "navigateLastPage": 1,
		        "lastPage": 1,
		        "firstPage": 1
		    }
		}
	 *
	 *
	 */
    @RequestMapping(value = "listAllPage", name = "班级列表", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/classes/listAllPage')")
    public RestResult getAllClassesListPage(@RequestParam(value = "schoolId", required = false) Long schoolId,
									@RequestParam(value = "phaseId", required = false) Long phaseId,
									@RequestParam(value = "gradeId", required = false) Long gradeId,
									@RequestParam(value = "isGraduated", required = false) Integer isGraduated,
									@RequestParam(name = "pageNum", required = false) Integer pageNum,
									@RequestParam(name = "pageSize", required = false) Integer pageSize) 
									throws Exception {

		PageInfo<ClassesVo> pageInfo = classesService.getAllClassesListPage(pageNum,pageSize,schoolId, phaseId, gradeId, isGraduated);
        return RestResult.ok(pageInfo);
    }
    
    /**
	 *
	 * @apiGroup classes
	 * @api {post} /classes/save 保存班级信息
	 * @apiVersion 1.0.0
	 * @apiName save
	 *
	 * @apiParam {SysClasses} classes 班级信息
	 * @apiParamExample 请求示例（json）
	 * {
		
			"classesId": 2,
			"classesNo": "2",
			"classesName": "八年级2班",
			"personalityName":"火箭班",
		    "schoolId": "1",
		    "phaseId": "2",
		    "gradeId": "8",
		    "enterYear": "2019",
		    "graduateYear": "2021",
		    "isGraduated":"0"
		
		}
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "classesId": 2
		    }
		}
	 *
	 *
	 */
	@RequestMapping(value = "save", name = "保存班级信息", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('/classes/save')")
	public RestResult save(@Valid @RequestBody SysClasses classes) throws Exception{
		
		classesService.saveClasses(classes);
		
		schoolService.updateSchoolClassesNum(classes.getSchoolId());
	
		return RestResult.ok();
	}
	
	/**
	 *
	 * @apiGroup classes
	 * @api {get} /classes/info  获取班级信息
	 * @apiVersion 1.0.0
	 * @apiName getClasses
	 *
	 * @apiParam {Long} classesId 班级id
	 * 
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "classesId": 1,
		        "classesNo": 1,
		        "classesName": "八年级1班",
		        "personalityName": "实验班",
		        "schoolId": 1,
		        "phaseId": 2,
		        "gradeId": 8,
		        "enterYear": "2018",
		        "graduateYear": "2021",
		        "isGraduated": 0,
		        "teacherId": 1,
		        "useType": 0,
		        "phaseName": "初中",
		        "gradeName": "八年级"
		    }
		}
	 *
	 *
	 */
	@RequestMapping(value = "info", name = "班级信息", method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('/classes/info')")
	public RestResult getClasses(@RequestParam Long classesId) throws Exception{
		ClassesVo classesVo = null;
		
		classesVo = classesService.getClassesById(classesId);
		
		return RestResult.ok(classesVo);
	}
	
	/**
	 *
	 * @apiGroup classes
	 * @api {post} /classes/delete 删除班级
	 * @apiVersion 1.0.0
	 * @apiName deleteClasses
	 *
	 * @apiParam {Map} dataMap 
	 * @apiParamExample 请求示例（json）
	 * {
		    "classesId": 2,
		    "schoolId": 1
		}
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": 1
		}
	 *
	 *
	 */
   @RequestMapping(value="/delete" , method = RequestMethod.POST)
   @PreAuthorize("hasAuthority('/classes/delete')")
   public RestResult deleteClasses(@RequestBody Map<String,Object> dataMap) throws Exception {
	   
	Long classesId = MapUtils.getLong(dataMap, "classesId");
	Long schoolId = MapUtils.getLong(dataMap, "schoolId");
	
	if (classesId == null || classesId<=0 || schoolId == null || schoolId<=0 )
	{
		return RestResult.error(ExceptionEnum.BADREQUEST.getCode(),ExceptionEnum.BADREQUEST.getMsg());
	}
   	    			
   	int isOk = classesService.deleteClasses(classesId);
   	if (isOk>0)
		{
   			schoolService.updateSchoolClassesNum(schoolId);
   		
			return RestResult.ok(isOk);
		}
		else
		{
			return RestResult.error();
		}
   }
   
   /**
	 *
	 * @apiGroup classes
	 * @api {post} /classes/saveTeacher 更新班级班主任
	 * @apiVersion 1.0.0
	 * @apiName saveClassesTeacher
	 *
	 * @apiParam {Map} dataMap 
	 * @apiParamExample 请求示例（json）
	 * {
		    "classesId": 2,
		    "teacherId": 11
		}
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": 1
		}
	 *
	 *
	 */
	 @RequestMapping(value = "/saveClassesTeacher", name = "更新班级班主任", method = RequestMethod.POST)
	 @PreAuthorize("hasAuthority('/classes/saveClassesTeacher')")
	 public RestResult saveClassesTeacher(@RequestBody Map<String,Object> dataMap) throws Exception{
				
		Long classesId = MapUtils.getLong(dataMap,"classesId");
		Long teacherId = MapUtils.getLong(dataMap,"teacherId");
        
	 	int isOk = classesService.saveClassesTeacher(classesId,teacherId);
	 	if (isOk>0)
			{
				return RestResult.ok(isOk);
			}
			else
			{
				return RestResult.error();
			}
	 }
   
	 /**
	 * 获取班级学科列表
	 * @apiGroup classes
	 * @api {get} /classes/classessubjects  获取班级学科列表
	 * @apiVersion 1.0.0
	 * @apiName getClassesSubjectList
	 *
	 * @apiParam {Long} classesId 班级id
	 * 
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": [
		        {
		            "classesId": 1,
		            "subjectId": 4,
		            "subjectName": "语文",
		            "phaseId": 2,
		            "sortNum": 3,
		            "phaseName": "初中",
		            "teachers": "学生1,东来"
		        },
		        {
		            "classesId": 1,
		            "subjectId": 5,
		            "subjectName": "数学",
		            "phaseId": 2,
		            "sortNum": 4,
		            "phaseName": "初中",
		            "teachers": "东来"
		        }
		    ]
		}
	 *
	 *
	 */
	@RequestMapping(value = "classessubjects", name = "获取班级学科列表", method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('/classes/classessubjects')")
	public RestResult getClassesSubjectList(@RequestParam(value = "phaseId") Long phaseId,
											@RequestParam(value = "classesId") Long classesId) throws Exception{
		
		List<ClassesSubjectVo> classesSubjectList = classesService.getClassesSubjectList(phaseId,classesId);
		
		return RestResult.ok(classesSubjectList);
	}
	
	
	/**
	 * 班级学科教师可选择列表
	 * @apiGroup classes
	 * @api {get} /classes/unselectedTeacher  班级学科教师可选择列表
	 * @apiVersion 1.0.0
	 * @apiName getUnselectedTeacherList
	 *
	 * @apiParam {Long} schoolId 学校id
	 * @apiParam {Long} phaseId 学段id
	 * @apiParam {Long} classesId 班级id
	 * @apiParam {Long} subjectId 学科id
	 * 
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": [
		        {
		            "teacherId": 9,
		            "teacherName": "张老师",
		            "classesId": null,
		            "subjectId": 5,
		            "subjectName": "数学",
		            "schoolId": 1,
		            "phaseId": 2,
		            "phaseName": "初中",
		            "teacherNo": ""
		        },
		        {
		            "teacherId": 14,
		            "teacherName": "李老师",
		            "classesId": null,
		            "subjectId": 5,
		            "subjectName": "数学",
		            "schoolId": 1,
		            "phaseId": 2,
		            "phaseName": "初中",
		            "teacherNo": ""
		        }
		    ]
		}
	 *
	 *
	 */
	@RequestMapping(value = "unselectedTeacher", name = "班级学科教师可选择列表", method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('/classes/unselectedTeacher')")
	public RestResult getUnselectedTeacherList(@RequestParam(value = "schoolId") Long schoolId,
												@RequestParam(value = "phaseId") Long phaseId,
												@RequestParam(value = "classesId") Long classesId,
												@RequestParam(value = "subjectId") Long subjectId,
												@RequestParam(value = "userName", required = false) String userName,
												@RequestParam(value = "selectids", required = false) String selectids
												) throws Exception{
		
		List<ClassesTeacherVo> classesTeacherList = classesService.getUnselectedTeacherList(schoolId,phaseId,classesId,subjectId,userName,selectids);
		
		return RestResult.ok(classesTeacherList);
	}
	
	/**
	 * 班级学科已任教教师列表
	 * @apiGroup classes
	 * @api {get} /classes/selectedTeacher  班级学科已任教教师列表
	 * @apiVersion 1.0.0
	 * @apiName getSelectedTeacherList
	 *
	 * @apiParam {Long} schoolId 学校id
	 * @apiParam {Long} phaseId 学段id
	 * @apiParam {Long} classesId 班级id
	 * @apiParam {Long} subjectId 学科id
	 * 
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": [
		        {
		            "teacherId": 1,
		            "teacherName": "东来",
		            "classesId": 1,
		            "subjectId": 5,
		            "subjectName": "数学",
		            "schoolId": 1,
		            "phaseId": 2,
		            "phaseName": "初中",
		            "teacherNo": "242564643"
		        }
		    ]
		}
	 *
	 *
	 */
	@RequestMapping(value = "selectedTeacher", name = "班级学科已任教教师列表", method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('/classes/selectedTeacher')")
	public RestResult getSelectedTeacherList(@RequestParam Long schoolId,
												@RequestParam Long phaseId,
												@RequestParam Long classesId,
												@RequestParam Long subjectId) throws Exception{
		
		List<ClassesTeacherVo> classesTeacherList = classesService.getSelectedTeacherList(schoolId,phaseId,classesId,subjectId);
		
		return RestResult.ok(classesTeacherList);
	}
	
	/**
     * 导入班级
     *
	 * @apiGroup classes
	 * @api {post} /classes/importclasses 导入班级
	 * @apiVersion 1.0.0
	 * @apiName importClasses
	 *
	 * @apiParam {MultipartFile} file 班级excel文件
	 * @apiParam {Long} schoolId 学校id
	 * @apiParam {HttpServletRequest} request 
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "statusinfo": "success"
		    }
		}
	 *
	 *
	 */
	@RequestMapping(value = "importclasses", name = "导入班级", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('/classes/importclasses')")
	public RestResult importClasses(MultipartFile file, 
			@RequestParam(value = "schoolId") Long schoolId, 
										HttpServletRequest request) throws Exception {
		
		Map<String, Object> result = classesService.importClasses(file, schoolId, request);

		return RestResult.ok(result);
		
	}

}
